<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CIBlog
 *
 * 使用codeigniter框架开发的一个博客。
 * 
 * @package     ciblog
 * @author      Eric
 * @copyright   Copyright (c) 2011 - 2012, Eric
 * @license     GNU General Public License 2.0
 * @link        http://www.hebaijun.com
 * @version     1.0
 */

 //----------------------------------------------------------

 /**
  * 取单页面的模型
  *
  * 
  *
  * @package     ciblog
  * @subpackage  Models
  * @category    Models
  * @author      Eric <baijun.he@163.com>
  * @link        http://www.hebaijun.com
  */
class Page_m extends CI_Model {

	/**
     * 构造函数
     * 
     * @access public
     * @return void
     */
    public function __construct()
    {
		parent::__construct();
		
		log_message('debug', "CIBlog: Page Model Class Initialized");

		//加载缓存适配器，如果memcached不支持 则用file类型缓存
		$this->load->driver('cache', array('adapter' => config_item('cache_adapter'), 'backup' => config_item('cache_backup')));

		$this->table_pages = $this->db->dbprefix('posts');
    }

	/**
     * 取得所有的页面信息，做导航等用处
     * 
     * @access public
	 * @param string 需要取得的字段
     * @return array 所有自动加载的系统设置
     */
    public function load_pages($param = '*', $search = array() )
    {
		//返回值
		$pages = FALSE;

		$this->db->select($param);

		if( ! empty( $search ) )
		{
			if(isset($search['page_id']) && ! empty($search['page_id']))
			{
				$this->db->where('post_id', $search['page_id']);
			}

			if(isset($search['limit']) && ! empty($search['limit']))
			{
				if(isset($search['offset']) && ! empty($search['offset']))
				{
					$this->db->limit($search['limit'], $search['offset']);
				}
				else
				{
					$this->db->limit($search['limit'], 0);
				}	
			}

			if(isset($search['orderby']) && ! empty($search['orderby']))
			{
				if(isset($search['order']) && ! empty($search['order']))
				{
					$this->db->order_by($search['orderby'], $search['order']);
				}
				else
				{
					$this->db->order_by($search['orderby']);
				}	
			}
		}

		$this->db->where('post_type', 'page');
		$this->db->where('post_status', 'publish');

		$this->db->from($this->table_pages);
		$sql = $this->db->_compile_select();

		$key = md5('page_'.$sql);
		$pages = $this->cache->get($key);
		if($pages === FALSE)
		{
			$query = $this->db->get();
			$pages = $query->result();
			$this->cache->save($key,$pages,config_item('cache_expire'));
		}

		//清空查询条件
		$this->db->_reset_select();

		return $pages;
    }	
}

/* End of file page.php */
/* Location: ./application/models/page.php */